import os
import shutil
import numpy as np
 
# img_dir = "sun/"
# xml_dir = "20201230_test_sun_image/"
# xml_out_dir = "20201230_test_sun_image_2"
# if not os.path.exists(xml_out_dir):
#     os.makedirs(xml_out_dir)

# if os.path.exists(img_dir):
#     for root, dirs, files in os.walk(img_dir):
#         #print("root:",root)#文件夹路径
#         #print("dirs:",dirs)#文件夹名称
#         #print("files:",files)#文件名
        
#         for file in files:#遍历每一个文件
#             filename = os.path.basename(file)#获取文件名
#             print(filename)
#             filename = filename[:-4]
            
#             xml_file = filename + '.jpg'#拼接得到文件路径
#             print(xml_file)
#             #shutil.move(xml_file, xml_dir)#移动文件
#             oldpath = os.path.join(xml_dir, xml_file)
#             shutil.copy(oldpath, xml_out_dir)


def check_path(path):
    if not os.path.exists(path):
        os.makedirs(path)

def get_file(data_path, xml_path, save_path, name):
    path = os.listdir(data_path)
    length  = len(path)
    num = np.ceil(length / 1000)

    for i in range(int(num)):
        tmp_path = os.path.join(save_path, name + "_" + str(i))
        check_path(tmp_path)

        if((i + 1) * 1000) >  length:
            end = length - i * 1000
        else:
            end = 1000

        for j in range(int(end)):
            move_path = os.path.join(data_path, path[i * 1000 + j])
            shutil.move(move_path, tmp_path)
            move_path = os.path.join(xml_path, path[i * 1000 + j].split('.jpg')[0] + ".xml")
            shutil.move(move_path, tmp_path)

save_path = '/home/chenwh/Documents/24bianli_data_image_ori/real_enviorment/20201230_test_sun_image_mean'
data_path = '/home/chenwh/Documents/24bianli_data_image_ori/real_enviorment/20201230_test_sun_image'
xml_path = '/home/chenwh/Documents/24bianli_data_image_ori/real_enviorment/20201230_test_sun_image_xml'
name = "20201230_test_sun_image_mean"

get_file(data_path, xml_path, save_path, name)